import time

from Common.Utils import blog_driver
from Common.FormHelper import FormHelper
from selenium.webdriver.common.by import By
import random
import datetime

class Register:
    url=""
    driver=""
    password=""
    name=""
    username=""
    def __init__(self):
        self.url="http://49.235.61.184:8080/user/register"
        self.driver=blog_driver.driver
        self.driver.get(self.url)
        self.name = ""  # 初始化实例属性
        self.password = ""
        self.driver.implicitly_wait(5)
        self.form = FormHelper(self.driver)
    #清理
    def clear(self):
        self.form.clear_fields(["#name", "#username", "#password", "#rePassword"])

    def commit(self,name,username,password,repassword):
        self.form.fill_fields({
            "#name": name,
            "#username": username,
            "#password": password,
            "#rePassword": repassword,
        })

    def Getpassword(self):
        len = random.randint(6, 10)
        min_num = 10 ** (len - 1)
        max_num = (10 ** len) - 1
        self.password = random.randint(min_num, max_num)

    def Getname(self):
        self.name = "测试账号" + datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")

    def _verify_error_message(self, css_selector, expected_text):
        self.form.verify_error_message(css_selector, expected_text, timeout=10)

    #登录成功
    def RegisterSucTest(self):
        self.clear()
        self.Getname()
        #随机生成6-10位数字长度
        self.Getpassword()
        self.username="123"+self.name
        self.commit(self.name,self.username,self.password,self.password)
        blog_driver.GetScreeShot()
        self.form.click("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")
        time.sleep(2)
        #证明登录成功
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")
        blog_driver.GetScreeShot()



    def RegisterFailTest(self,name="",username="",password="",repassword="",error_rules=None):
        #清空
        self.clear()
        #填充
        self.commit(name,username,password,repassword)
        #提交
        self.form.click("#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button")
        blog_driver.GetScreeShot()

        #验证所有错误提示
        if error_rules:
            for css_selector,expected_text in error_rules.items():
                self._verify_error_message(css_selector,expected_text)

        blog_driver.GetScreeShot()
        self.driver.get(self.url)

    #重复注册---信息已经存在
    def FaileTest1(self):
        self.RegisterSucTest()
        self.driver.back()

        self.RegisterFailTest(
            name=self.name,
            username="123"+self.name,
            password=self.password,
            repassword=self.password,
            error_rules={
                "#root > div > div.content___1k5Ro > div.main___19HXK > div > form > div.ant-alert.ant-alert-error > div > div": "账号已存在"
            }
        )

    #注册失败--全不填
    def FaileTest2(self):
        self.RegisterFailTest(name="",
                              username="",
                              password="",
                              repassword="",
                              error_rules={
                                  "#name_help > div":"用户名是必填项！",
                                  "#username_help > div":"登录账号不能为空",
                                  "#password_help > div":"密码是必填项！",
                                  "#rePassword_help > div":"请再次输入密码"
                              })

    #注册失败--姓名，密码为空
    def FaileTest3(self):
        self.Getname()
        self.Getpassword()
        self.RegisterFailTest(name="",
                              username="123"+self.name,
                              password="",
                              repassword=self.password,
                              error_rules={
                                  "#name_help > div": "用户名是必填项！",
                                  "#password_help > div": "密码是必填项！",
                              })

        #注册失败--姓名，账户为空
    def FaileTest4(self):
        self.Getpassword()
        self.RegisterFailTest(name="",
                                  username="",
                                  password=self.password,
                                  repassword=self.password,
                                  error_rules={
                                      "#name_help > div": "用户名是必填项！",
                                      "#username_help > div": "登录账号不能为空",
                                  })

      # 注册失败--账号，密码，密码确认为空
    def FaileTest5(self):
        self.Getname()
        self.RegisterFailTest(name=self.name,
                              username="",
                              password="",
                              repassword="",
                              error_rules={
                                  "#username_help > div": "登录账号不能为空",
                                  "#password_help > div": "密码是必填项！",
                                  "#rePassword_help > div": "请再次输入密码"
                              })

    # 注册失败--姓名，密码，密码确认为空
    def FaileTest6(self):
        self.Getname()
        self.RegisterFailTest(name="",
                              username=self.name,
                              password="",
                              repassword="",
                              error_rules={
                                  "#name_help > div":"用户名是必填项！",
                                  "#password_help > div":"密码是必填项！",
                                  "#rePassword_help > div":"请再次输入密码"
                              })

        # 注册失败--姓名，账号，密码为空
    def FaileTest7(self):
        self.Getpassword()
        self.RegisterFailTest(name="",
                              username="",
                              password="",
                              repassword=self.password,
                              error_rules={
                                  "#name_help > div": "用户名是必填项！",
                                  "#username_help > div": "登录账号不能为空",
                                  "#password_help > div": "密码是必填项！",
                              })

# register=Register()
# register.FaileTest1()
# register.FaileTest2()
# register.FaileTest3()
# register.FaileTest4()
# register.FaileTest5()
# register.FaileTest6()
# register.FaileTest7()



